/**
 * Author:walktree
 * walktree at gmail dot com
 * http://www.fluidea.cn/blog
 * Thanks for the the js script from  http://www.uza.lt/rightclick/
 * Worked in IE 7,FF 3 and Google Chrome
 */
package com.ibio8.javascript {
	
	import flash.external.ExternalInterface;
	import flash.utils.setTimeout;
	/**
	 * @example
	 * import org.fluidea.utils.RightClick;
	 * RightClick.init(yourCallBack);
	 * @private
	 */
	public class RightClick{
		/**
		 * 注意：
		 * 1 这些方法可能需要嵌入到页面中时才有效，否则可能会返回 null
		 * 2 由于 JS 的跨域问题，需要设置 'allowScriptAccess','always'
		 * 3 设置"wmode", "opaque"
		 */
		public static function init(callback:Function = null, ...rest):void {
			//check first
			if(ExternalInterface.available){
				var swfID:String = ExternalInterface.objectID;
				try {
					ExternalInterface.call("eval", 'var swfobj = document.getElementById("' + swfID 
					+ '");var parentID;if(swfobj.parentElement == undefined){parentID = swfobj.parentNode.id;}else{parentID = swfobj.parentElement.id;}');
				}catch (e:Error) {
					trace("RightClick::init->", e.message);
				}
				try {
					ExternalInterface.call("eval", 'var RightClick = {init: function () {this.FlashObjectID = "' + swfID
					+ '";this.FlashContainerID = parentID;	this.Cache = this.FlashObjectID;if(window.addEventListener) {window.addEventListener("mousedown", this.onGeckoMouse(), true);}else {document.getElementById(this.FlashContainerID).onmouseup = function() { document.getElementById(RightClick.FlashContainerID).releaseCapture(); },	document.oncontextmenu = function(){ if(window.event.srcElement.id == RightClick.FlashObjectID) { return false; } else { RightClick.Cache = "nan"; }},document.getElementById(this.FlashContainerID).onmousedown = RightClick.onIEMouse;	}	},onGeckoMouse: function(ev) {	return function(ev) { if (ev.button == 2) {RightClick.killEvents(ev);if(ev.target.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {;RightClick.call();}RightClick.Cache = ev.target.id;}  }},killEvents: function(eventObject) {	if(eventObject) {if (eventObject.stopPropagation) eventObject.stopPropagation();	if (eventObject.preventDefault) eventObject.preventDefault();	if (eventObject.preventCapture) eventObject.preventCapture();if (eventObject.preventBubble) eventObject.preventBubble();}},onIEMouse: function() {	if (event.button == 2) {if(window.event.srcElement.id == RightClick.FlashObjectID && RightClick.Cache == RightClick.FlashObjectID) {	RightClick.call(); 	}	document.getElementById(RightClick.FlashContainerID).setCapture();	if(window.event.srcElement.id)	RightClick.Cache = window.event.srcElement.id;	}},call: function() {document.getElementById(this.FlashObjectID).rightClick();	}}');
				}catch (e:Error) {
					trace("RightClick::init->", e.message);
				}
				//in some browsers, "eval" processing is so so so slow
				//setTimeout(fixBug, 100, callback, rest);
			}
		}
		
		private static function fixBug(func:Function, params:Array):void {
			try {
				ExternalInterface.call("RightClick.init");
			}catch (e:Error) {
				trace("RightClick::fixBug", e.message);
			}
			try {
				if (params.length == 0) {
					ExternalInterface.addCallback("rightClick", function():void { if ( func != null ) func();});
				}else {
					ExternalInterface.addCallback("rightClick", function():void { if ( func != null ) func(params);});
				}
			}catch (e:Error) {
				trace("RightClick::fixBug", e.message);
			}
		}
	}
}